﻿using System;
using System.Data.OleDb;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.IO;
using System.Text;
using Dukey.DBUtility;


public partial class Crm_EditProduct : BaseCrm
{ 
  
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dropshowtime.DataSource = GetSort("showtime");
            dropshowtime.DataTextField = "codetxt";
            dropshowtime.DataValueField = "codevalue";
            dropshowtime.DataBind();

           



            if (!string.IsNullOrEmpty(Request.QueryString["id"]))//如果是更新数据,则加载该记录.
            {
                string id = Request.QueryString["id"];

                string sql = "select * from Product where ID=@ID";
                OleDbDataReader dr = DbHelper.ExecuteReader(sql, new OleDbParameter[] { new OleDbParameter("@ID", id) });
                if (dr.Read())
                {
                    txtProductNO.Text = dr["ProductNo"].ToString();
                    txtProductName.Text = dr["Product_name"].ToString();
                  
                    dropshowtime.SelectedValue = dr["showtime"].ToString();
                   
                    dropSort.SelectedValue = dr["cateid"].ToString();
                    txtContent.Text = dr["contents"].ToString();
                    txtPrice.Text = dr["price"].ToString();
                    txtPublishTime.Text =Convert.ToDateTime(dr["cur_time"]).ToString("yyyy-M-d");
                    chkIsShow.Checked = dr["isshow"].ToString() == "0" ? false : true;
                }
                dr.Dispose();

            }
            else
            {
                txtPublishTime.Text = DateTime.Now.ToString("yyyy-M-d");                
            }
        }
    }


    

    protected void btnSubmit_Click(object sender, EventArgs e)
    {

        //上传文件
        string imageName = UploadFile();
        string filepath = txtDownloadFile.Text;
        string sql = "INSERT INTO [Product] ([ProductNo], [product_name],[showtime], [size], [cateId], [contents], [price], [cur_time],filepath,imgurl) VALUES (@ProductNo, @product_name, @showtime, @size,@cateId, @contents, @price, @cur_time,@imgurl,@filepath)";

        OleDbParameter[] paras =
        {          
            new OleDbParameter("@ProductNo",StringHelper.ReplaceBadChar(txtProductNO.Text)),
            new OleDbParameter("@product_name",StringHelper.ReplaceBadChar(txtProductName.Text)),
            new OleDbParameter("@showtime",StringHelper.ReplaceBadChar(dropshowtime.SelectedValue)),
            new OleDbParameter("@size",StringHelper.ReplaceBadChar(txtSize.Text)),
            new OleDbParameter("@cateId",dropSort.SelectedValue),
            new OleDbParameter("@contents",txtContent.Text),
            new OleDbParameter("@price",StringHelper.ReplaceBadChar(txtPrice.Text)),
            new OleDbParameter("@cur_time",StringHelper.ReplaceBadChar(txtPublishTime.Text)),
            new OleDbParameter("@imgurl",StringHelper.ReplaceBadChar(imageName)),
            new OleDbParameter("@filepath",StringHelper.ReplaceBadChar(filepath)),
            new OleDbParameter("@isshow",chkIsShow.Checked==true?1:0)          
                
        };

       


        if (!StringHelper.isNum(Request.QueryString["id"]))//插入新纪录
        {
            DbHelper.ExecuteSql(sql, paras);
            
        }
        else//更新纪录
        {
            string id =StringHelper.ReplaceBadChar(Request.QueryString["id"]);
            sql = "update [Product] set  [ProductNo]=@ProductNo, [product_name]=@product_name, [showtime]=@showtime, [size]=@size, [cateId]=@cateId, [contents]=@contents, [price]=@price, [cur_time]=@cur_time,imgurl=@imgurl,filepath=@filepath where ID=" + id + "";
            DbHelper.ExecuteSql(sql, paras);
        }
        lblResult.Text = "操作成功!";


    }

    #region 上传文件
    private string UploadFile()
    {
        string fileName = "";
        Boolean fileOk = false;
        string path = Server.MapPath("~/upload/");
        string httpPath = "~/upload/";
        //判断是否已经选取文件
        if (fileUpload.HasFile)
        {
            //取得文件的扩展名,并转换成小写
            string fileExtension = System.IO.Path.GetExtension(fileUpload.FileName).ToLower();
            //限定只能上传jpg和gif图片
            string[] allowExtension = { ".jpg", ".gif" };
            //对上传的文件的类型进行一个个匹对
            for (int i = 0; i < allowExtension.Length; i++)
            {
                if (fileExtension == allowExtension[i])
                {
                    fileOk = true;
                    break;
                }
            }
            //对上传文件的大小进行检测，限定文件最大不超过1M
            if (fileUpload.PostedFile.ContentLength > 1024000)
            {
                fileOk = false;
            }
            //最后的结果
            if (fileOk)
            {
                try
                {
                    fileName = StringHelper.GetRandomStr() + fileExtension;
                    fileUpload.PostedFile.SaveAs(path + fileName);                   

                    Response.Write("<script type=‘text/javascript‘>window.alert(‘上传成功‘)</script>");

                }
                catch
                {
                    Response.Write("<script type=‘text/javascript‘>window.alert(‘上传失败‘)</script>");
                }
            }
            else
            {
                Response.Write("<script type=‘text/javascript‘>window.alert(‘文件类型或者文件大小超出１Ｍ‘)</script>");
            }

        }
        return fileName;
    }
    #endregion



    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("editproduct.aspx");
    }

    private OleDbDataReader GetSort(string sortcode)
    {
        string sql = "select * from sys_params where [code]='" + sortcode + "'";
        return DbHelper.ExecuteReader(sql);
    }
}
